﻿using IRepositoryBll;
using IRepositoryDal;
using Microsoft.AspNetCore.Mvc;
using RepositoryEntity;
using RepositorySystem.Models;
using RepositorySystem.Models.CategoryViewModel;
using RepositorySystem.Models.ConsumableInfoViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace RepositorySystem.Controllers
{
    public class CategoryController : Controller
    {
        private ICategoryBll _categoryBll;
        private ICategoryDal _categoryDal;
        public CategoryController(ICategoryBll categoryBll , ICategoryDal categoryDal)
        {
            _categoryBll = categoryBll;
            _categoryDal = categoryDal;
        }

        /// <summary>
        /// 物资类别页面
        /// </summary>
        /// <returns></returns>
        public IActionResult CategoryListView()
        {
            return View();
        }

        /// <summary>
        /// 添加物资类型页面
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public IActionResult AddCategoryView(AddCategoryViewModel model)
        {
            return View();
        }

        /// <summary>
        /// 添加物资类型
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public IActionResult AddCategory(AddCategoryViewModel model)
        {
            CustomActionResult res = new CustomActionResult();
            if (model.CategoryName == null)
            {
                res.Msg = "类别名称不能为空";
                return Json(res);
            }

            Category category = new Category()
            {
                ID = Guid.NewGuid().ToString(),
                CategoryName = model.CategoryName,
                Description = model.Description,
                
                
                
            };
            bool isSuccess = _categoryBll.AddCategory(category);
            if (isSuccess)
            {
                res.IsSuccess = true;
                res.Msg = "成功";
                return Json(res);
            }
            else
            {
                return Json(res);
            }

        }

        public IActionResult GetCategoryList(int page, int limit, string categoryName)
        {
            int count;
            object category = _categoryBll.GetCategoryListByPage(page, limit, out count, categoryName);

            return Json(new
            {
                code = 0,
                msg = "成功",
                count = count,
                data = category
            });
        }

        /// <summary>
        /// 修改类型页面
        /// </summary>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        public IActionResult UpdateCategoryView(string categoryId)
        {
            return View();
        }

        /// <summary>
        /// 获取类型ID集合
        /// </summary>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        public IActionResult GetUpdateCategory(string categoryId)
        {
            CustomActionResult res = new CustomActionResult();
            Category category = _categoryBll.GetCategory(categoryId);
            res.IsSuccess = true;
            res.Msg = "成功";
            res.Status = 1;
            res.Data = new
            {
                Category = category
            };

            return Json(res);
        }

        /// <summary>
        /// 更新类型信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateCategory(UpdateCategoryViewModel model)
        {
            CustomActionResult res = new CustomActionResult();
            if (_categoryBll.UpdateCategory(model.ID,model.CategoryName,model.Description))
            {
                res.IsSuccess = true;
                res.Status = 1;
                res.Msg = "成功";

            }
            return Json(res);
        }

        /// <summary>
        /// 删除类型信息
        /// </summary>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        public IActionResult DeleteCategory(string categoryId)
        {
            CustomActionResult res = new CustomActionResult();
            if (_categoryBll.DeleteCategory(categoryId))
            {
                res.Status = 1;
                res.Msg = "成功";
                res.IsSuccess = true;
                return Json(res);
            }
            else
            {
                return Json(res);
            }
        }
    }
}
